package com.ly.hotel.view.common;

import java.lang.ref.WeakReference;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.common.SysConf;
import com.ly.hotel.msgevent.ActionTimeTickEvent;
import com.ly.hotel.msgevent.GetAreaEvent;
import com.ly.hotel.msgevent.GetWeatherEvent;
import com.ly.hotel.msgevent.HeaderUpdateEvent;
import com.ly.hotel.util.DateUtils;
import com.ly.hotel.util.LanguageUtil;
import com.ly.hotel.view.fragments.about.AboutDialog;

import de.greenrobot.event.EventBus;

/**
 * @author jouson
 */
public class Header extends LinearLayout {

	private TextView time;
	private TextView date;
	private TextView week;
	private static MyApplication app = (MyApplication) MyApplication.getContext();
	private TextView nowWN;
	private TextView nowWHot;
	private TextView myArea;
	private TextView room_text;
	private LinearLayout my_wifi_msg;
	private TextView wifiName;
	private TextView wifiPwd;
	private AboutDialog aboutDialog;


	// 更新页面显示的时间
	private Handler mHandler = new MessageHandler(this);

	public Header(Context context, int resId, String name) {
		super(context);
		init();
	}

	public Header(final Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init() {
		EventBus.getDefault().register(this);
		View view = LayoutInflater.from(getContext()).inflate(R.layout.header,this);
		time = view.findViewById(R.id.module_time);
		date = view.findViewById(R.id.module_date);
		week = view.findViewById(R.id.module_week);
		nowWN  = view.findViewById(R.id.weather_now_w);
		nowWHot = view.findViewById(R.id.weather_now_hot);
		myArea = view.findViewById(R.id.my_area);
		room_text = view.findViewById(R.id.my_roomNo);
		my_wifi_msg = view.findViewById(R.id.my_wifi_msg);
		wifiName = view.findViewById(R.id.wifi_name);
		wifiPwd = view.findViewById(R.id.password);
		if(SysConf.extendConfGetValue(SysConf.SYS_CONF_HEAD_DATA_SHOW,"1").equals("0")){
			nowWN.setVisibility(GONE);
			nowWHot.setVisibility(GONE);
			myArea.setVisibility(GONE);
			my_wifi_msg.setVisibility(GONE);
		}
		room_text.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(aboutDialog == null) {
					aboutDialog = new AboutDialog(MyApplication.getContext(), R.style.introDialog);
					aboutDialog.setCanceledOnTouchOutside(true);
					aboutDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
				}
				aboutDialog.show();
				aboutDialog.setRoomVer();
			}
		});

		showDataWeather();
	}

	private void showDataWeather(){
		Resources resources = getResources();// 获得res资源对象
		Configuration config = resources.getConfiguration();// 获得设置对象
		if(SysConf.extendConfGetValue(SysConf.SYS_CONF_HEAD_DATA_SHOW,"1").equals("1")){
			showWifi();
		}
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dateServer = null;
		String str = app.nowTime;

		try {
			dateServer = format.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if(dateServer != null) {
			time.setText(DateUtils.getTimeString(dateServer, resources));
			String w;
			String myDate = DateUtils.getDateString(dateServer, resources);
			if (config.locale.equals(Locale.SIMPLIFIED_CHINESE) || config.locale.equals(Locale.TRADITIONAL_CHINESE)
					|| config.locale.equals(Locale.CHINA)) {
				w = DateUtils.getWeekOfDateZH(dateServer);
			} else {
				w = DateUtils.getWeekOfDateEN(dateServer);
			}
			date.setText(myDate);
			week.setText(w);
			if (TextUtils.isEmpty(app.areaId)){
				EventBus.getDefault().post(new GetAreaEvent());
			}else {
                myArea.setText(LanguageUtil.getTextFromLocalLanguage(app.areaName));
				if (app.nowWeather != null) {
					String wn = LanguageUtil.getTextFromLocalLanguage(app.nowWeather.getName() + ";" + app.nowWeather.getNameEn());
					nowWN.setText(wn);
					nowWHot.setText(app.nowWeather.getTemperature() + "℃");
				} else {
					EventBus.getDefault().post(new GetWeatherEvent());
				}
			}
		}

	}

	//显示wifi信息
	private void showWifi(){
		if(my_wifi_msg != null){
			if (!TextUtils.isEmpty(app.wifiName) && !TextUtils.isEmpty(app.wifiPwd)){
				my_wifi_msg.setVisibility(VISIBLE);
				wifiName.setText(app.wifiName);
				wifiPwd.setText(app.wifiPwd);
			}
		}
	}

	//这个时间一分钟来一次，也就是每分钟更新一下时间
	public void onEventMainThread(ActionTimeTickEvent e) {
		if(getVisibility() == View.VISIBLE){
			if(mHandler.hasMessages(1)){
				mHandler.removeMessages(1);
			}
			mHandler.sendEmptyMessageDelayed(1, 100);
		}
	}

	public void onEventMainThread(HeaderUpdateEvent e) {
		updateRoomInfo();
	}

	// 更新本机的位置信息显示
	public void updateRoomInfo() {
		if (room_text != null) {
			String roomno = MyApplication.getDeviceInfo() == null ? "未登录" : MyApplication.getDeviceInfo().getName();
			//room_text.setText(roomno + getResources().getString(R.string.roomName));
			room_text.setText(roomno);
		}
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		EventBus.getDefault().unregister(this);
	}


	private static class MessageHandler extends Handler {
		private final WeakReference<Header> mView;

		MessageHandler(Header view) {
			mView = new WeakReference<>(view);
		}

		@Override
		public void handleMessage(Message msg) {
			Header view = mView.get();
	
			switch (msg.what) {
				case 1:
					view.showDataWeather();
					break;
			}
		}
	}
}

